FROM registry.access.redhat.com/ubi9/openjdk-21-runtime

# Add git commit label must be specified at build time using --build-arg GIT_COMMIT=dadadadadad
ARG GIT_COMMIT=unknown

LABEL git-commit=$GIT_COMMIT

USER root

ARG JAVA_TOOL_OPTIONS
ENV JAVA_TOOL_OPTIONS=${JAVA_TOOL_OPTIONS}

ARG TZ=Europe/Amsterdam
ENV TZ=${TZ}

ARG OR_SETUP_TYPE
ENV OR_SETUP_TYPE=${OR_SETUP_TYPE}

ARG OR_SSL_PORT=-1
ENV OR_SSL_PORT=${OR_SSL_PORT}

ARG OR_HOSTNAME=localhost
ENV OR_HOSTNAME=${OR_HOSTNAME}

ARG OR_EMAIL_HOST
ENV OR_EMAIL_HOST=${OR_EMAIL_HOST}

ARG OR_EMAIL_USER
ENV OR_EMAIL_USER=${OR_EMAIL_USER}

ARG OR_EMAIL_PORT=587
ENV OR_EMAIL_PORT=${OR_EMAIL_PORT}

ARG OR_EMAIL_TLS=true
ENV OR_EMAIL_TLS=${OR_EMAIL_TLS}

ARG OR_EMAIL_PROTOCOL=smtp
ENV OR_EMAIL_PROTOCOL=${OR_EMAIL_PROTOCOL}

ARG OR_EMAIL_FROM
ENV OR_EMAIL_FROM=${OR_EMAIL_FROM}

ARG OR_EMAIL_ADMIN
ENV OR_EMAIL_ADMIN=${OR_EMAIL_ADMIN}

ARG OR_EMAIL_X_HEADERS
ENV OR_EMAIL_X_HEADERS=${OR_EMAIL_X_HEADERS}

ARG OR_FIREBASE_CONFIG_FILE=/deployment/manager/fcm.json
ENV OR_FIREBASE_CONFIG_FILE=${OR_FIREBASE_CONFIG_FILE}

ARG OR_DEV_MODE=false
ENV OR_DEV_MODE=${OR_DEV_MODE}

ARG OR_SETUP_RUN_ON_RESTART=false
ENV OR_SETUP_RUN_ON_RESTART=${OR_SETUP_RUN_ON_RESTART}

ARG OR_WEBSERVER_LISTEN_HOST=0.0.0.0
ENV OR_WEBSERVER_LISTEN_HOST=${OR_WEBSERVER_LISTEN_HOST}

ARG OR_DB_VENDOR=postgres
ENV OR_DB_VENDOR=${OR_DB_VENDOR}

ARG OR_DB_HOST=postgresql
ENV OR_DB_HOST=${OR_DB_HOST}

ARG OR_DB_PORT=5432
ENV OR_DB_PORT=${OR_DB_PORT}

ARG OR_DB_NAME=openremote
ENV OR_DB_NAME=${OR_DB_NAME}

ARG OR_DB_SCHEMA=openremote
ENV OR_DB_SCHEMA=${OR_DB_SCHEMA}

ARG OR_DB_USER=postgres
ENV OR_DB_USER=${OR_DB_USER}

ARG OR_DB_POOL_MIN_SIZE=5
ENV OR_DB_POOL_MIN_SIZE=${OR_DB_POOL_MIN_SIZE}

ARG OR_DB_POOL_MAX_SIZE=20
ENV OR_DB_POOL_MAX_SIZE=${OR_DB_POOL_MAX_SIZE}

ARG OR_DB_CONNECTION_TIMEOUT_SECONDS=300
ENV OR_DB_CONNECTION_TIMEOUT_SECONDS=${OR_DB_CONNECTION_TIMEOUT_SECONDS}

ARG OR_DB_FLYWAY_OUT_OF_ORDER
ENV OR_DB_FLYWAY_OUT_OF_ORDER=${OR_DB_FLYWAY_OUT_OF_ORDER}

ARG OR_KEYCLOAK_HOST=keycloak
ENV OR_KEYCLOAK_HOST=${OR_KEYCLOAK_HOST}

ARG OR_KEYCLOAK_PORT=8080
ENV OR_KEYCLOAK_PORT=${OR_KEYCLOAK_PORT}

ARG OR_KEYCLOAK_GRANT_FILE=manager/keycloak-credentials.json
ENV OR_KEYCLOAK_GRANT_FILE=${OR_KEYCLOAK_GRANT_FILE}

ARG OR_APP_DOCROOT=/opt/web
ENV OR_APP_DOCROOT=${OR_APP_DOCROOT}

ARG OR_CUSTOM_APP_DOCROOT=/deployment/manager/app
ENV OR_CUSTOM_APP_DOCROOT=${OR_CUSTOM_APP_DOCROOT}

ARG OR_PROVISIONING_DOCROOT=/deployment/manager/provisioning
ENV OR_PROVISIONING_DOCROOT=${OR_PROVISIONING_DOCROOT}

ARG OR_ROOT_REDIRECT_PATH=/manager
ENV OR_ROOT_REDIRECT_PATH=${OR_ROOT_REDIRECT_PATH}

ARG OR_LOGGING_CONFIG_FILE
ENV OR_LOGGING_CONFIG_FILE=${OR_LOGGING_CONFIG_FILE}

ARG OR_MAP_TILES_PATH=/deployment.local/mapdata/mapdata.mbtiles
ENV OR_MAP_TILES_PATH=${OR_MAP_TILES_PATH}

ARG OR_MAP_SETTINGS_PATH=/deployment/map/mapsettings.json
ENV OR_MAP_SETTINGS_PATH=${OR_MAP_SETTINGS_PATH}

ARG OR_MAP_TILESERVER_HOST
ENV OR_MAP_TILESERVER_HOST=${OR_MAP_TILESERVER_HOST}

ARG OR_MAP_TILESERVER_PORT=8082
ENV OR_MAP_TILESERVER_PORT=${OR_MAP_TILESERVER_PORT}

ARG OR_MAP_TILESERVER_REQUEST_TIMEOUT=10000
ENV OR_MAP_TILESERVER_REQUEST_TIMEOUT=${OR_MAP_TILESERVER_REQUEST_TIMEOUT}

ARG OR_IDENTITY_PROVIDER=keycloak
ENV OR_IDENTITY_PROVIDER=${OR_IDENTITY_PROVIDER}

ARG OR_IDENTITY_SESSION_MAX_MINUTES=1440
ENV OR_IDENTITY_SESSION_MAX_MINUTES=${OR_IDENTITY_SESSION_MAX_MINUTES}

ARG OR_IDENTITY_SESSION_OFFLINE_TIMEOUT_MINUTES=2628000
ENV OR_IDENTITY_SESSION_OFFLINE_TIMEOUT_MINUTES=${OR_IDENTITY_SESSION_OFFLINE_TIMEOUT_MINUTES}

ARG OR_ATTRIBUTE_EVENT_THREADS
ENV OR_ATTRIBUTE_EVENT_THREADS=${OR_ATTRIBUTE_EVENT_THREADS}

ARG OR_STORAGE_DIR=/storage
ENV OR_STORAGE_DIR=${OR_STORAGE_DIR}

ARG OR_GATEWAY_TUNNEL_LOCALHOST_REWRITE=172.17.0.1
ENV OR_GATEWAY_TUNNEL_LOCALHOST_REWRITE=${OR_GATEWAY_TUNNEL_LOCALHOST_REWRITE}

ARG OR_JAVA_OPTS="-Xms500m -Xmx2g \
    -XX:NativeMemoryTracking=summary \
    -Xlog:all=warning:stdout:uptime,level,tags \
    -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/dump.hprof"
ENV OR_JAVA_OPTS=${OR_JAVA_OPTS}

# OpenMetrics _created series breaks cloudwatch prometheus scraper
ARG PROMETHEUS_DISABLE_CREATED_SERIES=true
ENV PROMETHEUS_DISABLE_CREATED_SERIES=${PROMETHEUS_DISABLE_CREATED_SERIES}

RUN mkdir -p /deployment/manager/extensions
RUN mkdir -p /storage/logs

WORKDIR /opt/app

ADD lib /opt/app/lib
ADD web /opt/web
ADD map /opt/map
ADD deployment /deployment

EXPOSE 8080
EXPOSE 1883

HEALTHCHECK --interval=5s --timeout=60s --start-period=5s --retries=120 CMD curl --fail --silent http://localhost:8080 || exit 1

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
ENTRYPOINT ["/entrypoint.sh"]
